﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using SwenAssigment.Classes;
using System.IO;

namespace SwenAssigment
{
    public partial class Site1 : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            string path = Path.GetFileName(Request.Url.AbsoluteUri).ToLower();
            string role = "";
            //Response.Write("<script>window.alert('" + path + "')</script>");
            if (path.Contains("?") == true)
            {
                path = path.Substring(0, path.IndexOf("?"));
            }
            if (Session["login"] == null)
            {

                pllogout.Visible = false;
                plLogin.Visible = true;
            }
            else
            {
                pllogout.Visible = true;
                plLogin.Visible = false;

                role = ((UserClass)Session["login"]).Role;
                lbllogintext.Text = "Welcome " + ((UserClass)Session["login"]).UserName + ", Role:" + ((UserClass)Session["login"]).Role;
                if (((UserClass)Session["login"]).Role == "Admin")
                {
                    Panel1.Visible = true;
                    Panel2.Visible = true;
                }
                else if (((UserClass)Session["login"]).Role == "Management")
                {
                    Panel2.Visible = true;
                }
            }

            if (ACList.AccessRight(path, role) == true)
            {

            }
            else
            {
                Response.Redirect("~/Unauthorized.html");
            }
        }
        protected void Button1_Click1(object sender, EventArgs e)
        {
            String login = JcUser.Login(txtName.Text, SecureFunction.hashvalue(txtPassword.Text));
            if (login == "lock")
            {
                //Response.Write("<script>window.alert('Account locked, Please contact admin to unlock')</script>");
                ClientScriptManager cs = Page.ClientScript;
                cs.RegisterStartupScript(this.GetType(), "PopupScript", "alert('Account locked, Please contact admin to unlock');", true);
            }
            else if (login == "fail")
            {
                ClientScriptManager cs = Page.ClientScript;
                cs.RegisterStartupScript(this.GetType(), "PopupScript", "alert('login fail');", true);
            }
            else
            {
                UserClass user = new UserClass();
                //(String)rdr["sUserName"], (String)rdr["Role"]
                user.UserName = txtName.Text;
                user.Role = login;
                Session["login"] = user;
                JcUser.Logged(user.UserName);
                //Response.Write("<script>window.alert('" + Request.Url.AbsolutePath+ "')</script>");
                //Response.Redirect(Request.Url.AbsoluteUri + "?hah=3");
                Response.Redirect(Request.Url.AbsoluteUri);

            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Session["login"] = null;
            Response.Redirect("~/default.aspx");
        }
    }
}
